<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2019/12/23
// +----------------------------------------------------------------------

namespace app\common\services\wechattemplate;

use app\common\facade\Arr;
use app\common\facade\TransItem;
use app\common\services\WechatTemplateService;
use think\Db;

/**
 * 兼职 -- 微信模版消息服务类
 * Class JobTemplateService
 * @package app\common\services\wechattemplate
 * @author feraner<feraner@163.com>
 * @date 2019/12/23
 */
class JobTemplateService extends WechatTemplateService
{

    public function __construct()
    {
        parent::__construct();

    }


    /**
     * 发送微信模版信息给 兼职审核人员审核
     * @param $jobInfo
     *
     * @return bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendAuditJob($jobInfo)
    {
        $this->clearSetData()
            ->set('template', 'job_check_remind')
            ->set('url', 'index/Jobs/auditJob?id=' . Arr::get($jobInfo, 'id'))
            ->set('first', '您有一条新提交的兼职需要审核！')
            ->set('keyword1', Arr::get($jobInfo, 'title'))
            ->set('keyword2', date('Y-m-d H:i', Arr::get($jobInfo, 'updated_at', time())))
            ->set('remark', '请尽快审核该兼职，谢谢');
        $checkJobUser = $this->getAgencyCheckUsers(
            Arr::get($jobInfo, 'provinces_id'),
            Arr::get($jobInfo, 'city_id'),
            Arr::get($jobInfo, 'county_id'),
            1
        );
        if ($jobCheckUserArr = Arr::get($checkJobUser, 'jobCheckUsers', [])) {
            foreach ($jobCheckUserArr as $item) {
                $result = $this->set('toUser', Arr::get($item, 'wechat_openid'))
                    ->send();
            }
        }
        return true;
    }


    /**
     * 通知用户 发布兼职审核成功信息
     * @param $jobInfo
     *
     * @return bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendAuditJobSuccess($jobInfo)
    {
        $userInfo = Db::name('user')
            ->where('id', Arr::get($jobInfo, 'user_id', 0))
            ->field('id,wechat_openid,name,truename')
            ->find() ?: [];
        if ($userInfo) {
            $this->clearSetData()
                ->set('template', 'check_job_success')
                ->set('url', 'index/Jobs/jobDetail?id=' . Arr::get($jobInfo, 'id'))
                ->set('first', '您提交的兼职信息已经审核通过了！')
                ->set('keyword1', Arr::get($jobInfo, 'title', ''))
                ->set('keyword2', '审核通过')
                ->set('remark', '审核已通过,点击查看兼职页面!')
                ->set('toUser', Arr::get($userInfo, 'wechat_openid'))
                ->send();
        }
        return true;
    }



    /**
     * 通知用户 发布兼职审核失败信息
     * @param $jobInfo
     *
     * @return bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendAuditJobFalse($jobInfo, $message)
    {
        $userInfo = Db::name('user')
            ->where('id', Arr::get($jobInfo, 'user_id', 0))
            ->field('id,wechat_openid,name,truename')
            ->find() ?: [];
        if ($userInfo) {
            $this->clearSetData()
                ->set('template', 'check_job_false')
                ->set('url', 'index/Usercenter/publishJobList?type=0')
                ->set('first', '您提交的兼职没有通过审核!')
                ->set('keyword1', Arr::get($userInfo, 'truename', Arr::get($userInfo, 'name')))
                ->set('keyword2', Arr::get($jobInfo, 'title'))
                ->set('keyword3', $message)
                ->set('keyword4', date('Y-m-d H:i'))
                ->set('remark', '请按照要求修改后再提交审核!')
                ->set('toUser', Arr::get($userInfo, 'wechat_openid'))
                ->send();
        }
        return true;
    }


    /**
     * 报名审核通过 -- 发送给报名人员通知信息
     * @param $jobInfo
     * @param $applyInfo
     *
     * @return bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendCheckJobPass($jobInfo, $applyInfo)
    {
        $userInfo = Db::name('user')
            ->where('id', Arr::get($applyInfo, 'user_id', 0))
            ->field('id,wechat_openid,name,truename')
            ->find() ?: [];
        if ($userInfo) {
            $this->clearSetData()
                ->set('template', 'apply_job_success')
                ->set('url', 'index/Usercenter/joinJobList?type=2')
                ->set('first', '恭喜您，您投递的' . Arr::get($jobInfo, 'title', '岗位') . '已被录用!')
                ->set('keyword1', Arr::get($jobInfo, 'salary') . TransItem::transMoneyUnit(Arr::get($jobInfo, 'salary_unit')))
                ->set('keyword2', date('Y-m-d', Arr::get($jobInfo, 'work_time')))
                ->set('keyword3', Arr::get($jobInfo, 'provinces_name') . Arr::get($jobInfo, 'address'))
                ->set('remark', '兼职联系方式：' . Arr::get($jobInfo, 'mobile') . ' ,请及时与招聘方联系!')
                ->set('toUser', Arr::get($userInfo, 'wechat_openid'))
                ->send();
        }
        return true;
    }


    /**
     * 报名兼职审核失败通知 -- 发送给报名人员通知信息
     * @param $jobInfo
     * @param $applyInfo
     * @param $postData
     *
     * @return bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendCheckJobNoPass($jobInfo, $applyInfo, $postData)
    {
        $userInfo = Db::name('user')
            ->where('id', Arr::get($applyInfo, 'user_id', 0))
            ->field('id,wechat_openid,name,truename')
            ->find() ?: [];
        if ($userInfo) {
            $this->clearSetData()
                ->set('template', 'apply_job_false')
                ->set('url', 'index/Usercenter/joinJobList?type=3')
                ->set('first', '抱歉，您报名的兼职[' . Arr::get($jobInfo, 'title', '岗位') . ']未被录取')
                ->set('keyword1', Arr::get($jobInfo, 'title'))
                ->set('keyword2', Arr::get($postData, 'message'))
                ->set('remark', '温馨提示：您可查看更多兼职，我们竭力为您最优质的服务。')
                ->set('toUser', Arr::get($userInfo, 'wechat_openid'))
                ->send();
        }
        return true;
    }


    /**
     * 通知发布人有人报名兼职 -- 及时去审核报名人
     * @param $jobInfo
     * @param $userInfo
     *
     * @return bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendUserApplyJob($jobInfo, $userInfo)
    {
        $publishUserInfo = Db::name('user')
            ->where('id', Arr::get($jobInfo, 'user_id'))
            ->field('id,wechat_openid,name,truename')
            ->find() ?: [];
        if ($publishUserInfo) {
            $schoolInfo = Db::name('school')
                ->where('id', Arr::get($userInfo['extend'], 'education_school', 0))
                ->find() ?: [];
            $this->clearSetData()
                ->set('template', 'people_apply_job')
                ->set('url', 'index/Jobs/applyUserList?jobId=' . Arr::get($jobInfo, 'id'))
                ->set('first', '您发布的兼职有人报名啦,快去审核吧！')
                ->set('keyword1', Arr::get($jobInfo, 'title'))
                ->set('keyword2', Arr::get($userInfo, 'truename', Arr::get($userInfo, 'name')))
                ->set('keyword3', Arr::get($schoolInfo, 'name', '无'))
                ->set('keyword4', Arr::get($userInfo, 'mobile'))
                ->set('keyword5', '暂无')
                ->set('remark', '请及时审核，并尽快联系确认！')
                ->set('toUser', Arr::get($publishUserInfo, 'wechat_openid'))
                ->send();
        }
        return true;
    }


    /**
     * 用户取消报名 -- 发送给 兼职发布人信息通知
     * @param $applyJobInfo
     * @param $msg
     *
     * @return bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendUserCancelApplyJob($applyJobInfo, $msg)
    {
        $jobInfo = Db::name('jobs')
            ->where('id', Arr::get($applyJobInfo, 'job_id'))
            ->find() ?: [];
        $publishUserInfo =  Db::name('user')
            ->where('id', Arr::get($applyJobInfo, 'publisher_id'))
            ->field('id,wechat_openid,name,truename')
            ->find() ?: [];
        $applyUserInfo = Db::name('user')
            ->where('id', Arr::get($applyJobInfo, 'user_id'))
            ->find() ?: [];
        if ($jobInfo && $publishUserInfo && $applyUserInfo) {
            $this->clearSetData()
                ->set('template', 'cancel_apply_job')
                ->set('url', 'index/Jobs/applyUserList?jobId=' . Arr::get($jobInfo, 'id'))
                ->set('first', '您好！' . Arr::get($applyUserInfo, 'truename', Arr::get($applyUserInfo, 'name')) .
                    '取消了您发布的兼职工作报名！')
                ->set('keyword1', Arr::get($jobInfo, 'title'))
                ->set('keyword2', Arr::get($applyUserInfo, 'truename', Arr::get($applyUserInfo, 'name')))
                ->set('keyword3', Arr::get($applyUserInfo, 'mobile', '无'))
                ->set('keyword4', date('Y-m-d H:i'))
                ->set('remark', '取消原因：' . $msg)
                ->set('toUser', Arr::get($publishUserInfo, 'wechat_openid'))
                ->send();
        }
        return true;
    }


    /**
     * 通知兼职发布人 -- 兼职报名人生已满
     * @param $jobInfo
     *
     * @return bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendPublishUserJobWorkerIsOk($jobInfo)
    {
        $publishUserInfo =  Db::name('user')
            ->where('id', Arr::get($jobInfo, 'user_id'))
            ->field('id,wechat_openid,name,truename')
            ->find() ?: [];
        if ($publishUserInfo) {
            $this->clearSetData()
                ->set('template', 'job_num_ok')
                ->set('url', 'index/Jobs/applyUserList?jobId=' . Arr::get($jobInfo, 'id'))
                ->set('first', '恭喜您！您发布的兼职人数已满!')
                ->set('keyword1', Arr::get($jobInfo, 'title'))
                ->set('keyword2', Arr::get($jobInfo, 'need_worker') . '(人数已满)')
                ->set('remark', '请查看您发布的兼职报名情况，并关闭兼职报名通道')
                ->set('toUser', Arr::get($publishUserInfo, 'wechat_openid'))
                ->send();
        }
        return true;
    }



}
